<!DOCTYPE html>












  


<html class="theme-next pisces use-motion" lang="zh-CN">
<head><meta name="generator" content="Hexo 3.8.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">


























<link rel="stylesheet" href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2">

<link rel="stylesheet" href="/css/main.css?v=7.0.0">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/Peppa.png?v=7.0.0">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/Peppa.jpg?v=7.0.0">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/Peppa.jpg?v=7.0.0">








<script id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Pisces',
    version: '7.0.0',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: false,
    fastclick: false,
    lazyload: false,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>


  




  <meta name="description" content="1 前言1.1 生产生活随着科学技术的飞速发展及计算机在国民经济各个领域中的普遍运用，计算机辅助设计，即CAD越来越为人们所重视。当前的CAD工作中，计算机远远不只是一种高效的计算工具，它已成为人们进行创造性设计活动的得力助手甚至参谋。计算几何作为CAD的基础理论之一，主要研究内容是几何形体的数学描述和计算机表述；它同计算机辅助几何设计，即CAGD有着十分密切的关系。而CAGD是由微分几何、代数几">
<meta name="keywords" content="ACM,计算几何">
<meta property="og:type" content="article">
<meta property="og:title" content="ACM几何基础篇">
<meta property="og:url" content="https://linxi99.gitee.io/20190211/ACM几何基础篇/index.html">
<meta property="og:site_name" content="linxi&#39;s dream">
<meta property="og:description" content="1 前言1.1 生产生活随着科学技术的飞速发展及计算机在国民经济各个领域中的普遍运用，计算机辅助设计，即CAD越来越为人们所重视。当前的CAD工作中，计算机远远不只是一种高效的计算工具，它已成为人们进行创造性设计活动的得力助手甚至参谋。计算几何作为CAD的基础理论之一，主要研究内容是几何形体的数学描述和计算机表述；它同计算机辅助几何设计，即CAGD有着十分密切的关系。而CAGD是由微分几何、代数几">
<meta property="og:locale" content="zh-CN">
<meta property="og:updated_time" content="2019-11-29T14:06:33.402Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="ACM几何基础篇">
<meta name="twitter:description" content="1 前言1.1 生产生活随着科学技术的飞速发展及计算机在国民经济各个领域中的普遍运用，计算机辅助设计，即CAD越来越为人们所重视。当前的CAD工作中，计算机远远不只是一种高效的计算工具，它已成为人们进行创造性设计活动的得力助手甚至参谋。计算几何作为CAD的基础理论之一，主要研究内容是几何形体的数学描述和计算机表述；它同计算机辅助几何设计，即CAGD有着十分密切的关系。而CAGD是由微分几何、代数几">






  <link rel="canonical" href="https://linxi99.gitee.io/20190211/ACM几何基础篇/">



<script id="page.configurations">
  CONFIG.page = {
    sidebar: "",
  };
</script>

  <title>ACM几何基础篇 | linxi's dream</title>
  






  <script>
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?b4099cd89c9f33abab06a03748dac801";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>







  <noscript>
  <style>
  .use-motion .motion-element,
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-title { opacity: initial; }

  .use-motion .logo,
  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope="" itemtype="http://schema.org/WebPage" lang="zh-CN">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope="" itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta">
    

    <div class="custom-logo-site-title">
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">linxi's dream</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
    
      
        <p class="site-subtitle">穷且益坚，不坠青云之志</p>
      
    
    
  </div>

  <div class="site-nav-toggle">
    <button aria-label="切换导航栏">
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>



<nav class="site-nav">
  
    <ul id="menu" class="menu">
      
        
        
        
          
          <li class="menu-item menu-item-home">

    
    
    
      
    

    

    <a href="/" rel="section"><i class="menu-item-icon fa fa-fw fa-home"></i> <br>首页</a>

  </li>
        
        
        
          
          <li class="menu-item menu-item-tags">

    
    
    
      
    

    

    <a href="/tags/" rel="section"><i class="menu-item-icon fa fa-fw fa-tags"></i> <br>标签</a>

  </li>
        
        
        
          
          <li class="menu-item menu-item-categories">

    
    
    
      
    

    

    <a href="/categories/" rel="section"><i class="menu-item-icon fa fa-fw fa-th"></i> <br>分类</a>

  </li>
        
        
        
          
          <li class="menu-item menu-item-archives">

    
    
    
      
    

    

    <a href="/archives/" rel="section"><i class="menu-item-icon fa fa-fw fa-archive"></i> <br>归档</a>

  </li>

      
      
        <li class="menu-item menu-item-search">
          
            <a href="javascript:;" class="popup-trigger">
          
            
              <i class="menu-item-icon fa fa-search fa-fw"></i> <br>搜索</a>
        </li>
      
    </ul>
  

  

  
    <div class="site-search">
      
  <div class="popup search-popup local-search-popup">
  <div class="local-search-header clearfix">
    <span class="search-icon">
      <i class="fa fa-search"></i>
    </span>
    <span class="popup-btn-close">
      <i class="fa fa-times-circle"></i>
    </span>
    <div class="local-search-input-wrapper">
      <input autocomplete="off" placeholder="搜索..." spellcheck="false" type="text" id="local-search-input">
    </div>
  </div>
  <div id="local-search-result"></div>
</div>



    </div>
  
</nav>



  



</div>
    </header>

    


    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          
            

          
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  

  <article class="post post-type-normal" itemscope="" itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://linxi99.gitee.io/20190211/ACM几何基础篇/">

    <span hidden itemprop="author" itemscope="" itemtype="http://schema.org/Person">
      <meta itemprop="name" content="linxi">
      <meta itemprop="description" content="stay hungry, stay foolish">
      <meta itemprop="image" content="/images/star.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope="" itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="linxi's dream">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">ACM几何基础篇

              
            
          </h1>
        

        <div class="post-meta">
          <span class="post-time">

            
            
            

            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              

              
                
              

              <time title="创建时间：2019-02-11 16:59:43" itemprop="dateCreated datePublished" datetime="2019-02-11T16:59:43+08:00">2019-02-11</time>
            

            
              

              
                
                <span class="post-meta-divider">|</span>
                

                <span class="post-meta-item-icon">
                  <i class="fa fa-calendar-check-o"></i>
                </span>
                
                  <span class="post-meta-item-text">更新于</span>
                
                <time title="修改时间：2019-11-29 22:06:33" itemprop="dateModified" datetime="2019-11-29T22:06:33+08:00">2019-11-29</time>
              
            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope="" itemtype="http://schema.org/Thing"><a href="/categories/计算几何瞎暴力/" itemprop="url" rel="index"><span itemprop="name">计算几何瞎暴力</span></a></span>

                
                
              
            </span>
          

          
            
            
              
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
            
                <span class="post-meta-item-text">评论数：</span>
                <a href="/20190211/ACM几何基础篇/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count valine-comment-count" data-xid="/20190211/ACM几何基础篇/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          
            <span id="/20190211/ACM几何基础篇/" class="leancloud_visitors" data-flag-title="ACM几何基础篇">
              <span class="post-meta-divider">|</span>
              <span class="post-meta-item-icon">
                <i class="fa fa-eye"></i>
              </span>
              
                <span class="post-meta-item-text">阅读次数：</span>
              
                <span class="leancloud-visitors-count"></span>
            </span>
            <span class="post-meta-divider">|
            </span> <span title="post.wordcount">
            <span class="post-meta-item-icon">
                <i class="fa fa-file-word-o"></i>
            </span>字数： 5.6k
            </span>
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h2 id="1-前言"><a href="#1-前言" class="headerlink" title="1 前言"></a>1 前言</h2><h3 id="1-1-生产生活"><a href="#1-1-生产生活" class="headerlink" title="1.1 生产生活"></a>1.1 生产生活</h3><p>随着科学技术的飞速发展及计算机在国民经济各个领域中的普遍运用，计算机辅助设计，即CAD越来越为人们所重视。当前的CAD工作中，计算机远远不只是一种高效的计算工具，它已成为人们进行创造性设计活动的得力助手甚至参谋。计算几何作为CAD的基础理论之一，主要研究内容是几何形体的数学描述和计算机表述；它同计算机辅助几何设计，即CAGD有着十分密切的关系。而CAGD是由微分几何、代数几何、数值计算、逼近论、拓扑学以及数控技术等形成的一门新兴边缘学科，其主要研究对象和内容是对自由形曲线、曲面的数学描述、设计、分析及图形的显示、处理等。</p>
<h3 id="1-2-ICPC竞赛"><a href="#1-2-ICPC竞赛" class="headerlink" title="1.2 ICPC竞赛"></a>1.2 ICPC竞赛</h3><ul>
<li><p>在竞赛中，计算几何相比于其他部分来说是比较独立的</p>
</li>
<li><p>曾出现过其与图论和动态规划相结合的题目，计算几何与其他的知识点很少有过多的结合</p>
</li>
<li><p>计算几何的题目难度不会很大，但也永远不会成为最水的题</p>
</li>
<li><p>计算几何题目具有代码量大、特殊情况多。精度问题难以控制等特点</p>
</li>
</ul>
<a id="more"></a>
<h2 id="2-准备知识"><a href="#2-准备知识" class="headerlink" title="2 准备知识"></a>2 准备知识</h2><h3 id="2-1-头文件及函数及常量"><a href="#2-1-头文件及函数及常量" class="headerlink" title="2.1 头文件及函数及常量"></a>2.1 头文件及函数及常量</h3><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;cstdio&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;cmath&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> <span class="keyword">double</span> pi = <span class="built_in">acos</span>(<span class="number">-1.0</span>);</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">double</span> inf = <span class="number">1e100</span>;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">double</span> eps = <span class="number">1e-6</span>;</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">sgn</span><span class="params">(<span class="keyword">double</span> d)</span></span>&#123;</span><br><span class="line">    <span class="keyword">if</span>(<span class="built_in">fabs</span>(d) &lt; eps)</span><br><span class="line">        <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">    <span class="keyword">if</span>(d &gt; <span class="number">0</span>)</span><br><span class="line">        <span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line">    <span class="keyword">return</span> <span class="number">-1</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    <span class="keyword">double</span> x = <span class="number">1.49999</span>;</span><br><span class="line">    <span class="keyword">int</span> fx = <span class="built_in">floor</span>(x);<span class="comment">//向下取整函数</span></span><br><span class="line">    <span class="keyword">int</span> cx = <span class="built_in">ceil</span>(x);<span class="comment">//向上取整函数</span></span><br><span class="line">    <span class="keyword">int</span> rx = round(x);<span class="comment">//四舍五入函数</span></span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">"%f %d %d %d\n"</span>, x, fx, cx, rx);</span><br><span class="line">    <span class="comment">//输出结果 1.499990 1 2 1</span></span><br><span class="line">    <span class="keyword">return</span>  <span class="number">0</span> ;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="2-2-浮点误差"><a href="#2-2-浮点误差" class="headerlink" title="2.2 浮点误差"></a>2.2 浮点误差</h3><p>计算几何中一般来说使用$double$类型比较频繁，该用实数是就用$double$，$float$容易失去精度</p>
<p>$double$类型读入时应用$\%lf$占位符</p>
<p>而输出时应用$\%f$占位符</p>
<h4 id="2-2-1-计算误差"><a href="#2-2-1-计算误差" class="headerlink" title="2.2.1 计算误差"></a>2.2.1 计算误差</h4><p>尽量少用三角函数、除法、开方、求幂、取对数运算</p>
<ul>
<li>尽量把公式整理成使用的以上操作最少的形式</li>
<li><p>$1.0/2.0<em>3.0/4.0</em>5.0 = (1.0<em>3.0</em>5.0)/(2.0*4.0)$</p>
</li>
<li><p>在不溢出的情况下将除式比较转化为乘式比较</p>
</li>
<li>$\frac {a}{b} &gt; c \Leftrightarrow  a &gt; bc$</li>
</ul>
<p>在使用除法、开根号、和三角函数的时候，我们要考虑由浮点误差运算产生的代价</p>
<h4 id="2-2-2-判等"><a href="#2-2-2-判等" class="headerlink" title="2.2.2 判等"></a>2.2.2 判等</h4><p>不要直接用等号判断浮点数是否相等！<br>不要直接用等号判断浮点数是否相等！<br>不要直接用等号判断浮点数是否相等！</p>
<p>同样的一个数$1.5$如果从不同的途径计算得来，它们的储存方式可能会是$a = 1.4999999$ 与 $b = 1.5000001$，此时使用$a == b$判断将返回$false$</p>
<h4 id="2-2-2-1-解决方案-1-误差判别法"><a href="#2-2-2-1-解决方案-1-误差判别法" class="headerlink" title="2.2.2.1 解决方案 1 误差判别法"></a>2.2.2.1 解决方案 1 误差判别法</h4><p>借助$\varepsilon$也就是上面定义的常量eps</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> <span class="keyword">double</span> eps = <span class="number">1e-9</span>;</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">dcmp</span><span class="params">(<span class="keyword">double</span> x, <span class="keyword">double</span> y)</span></span>&#123;</span><br><span class="line">    <span class="keyword">if</span>(<span class="built_in">fabs</span>(x - y) &lt; eps)</span><br><span class="line">        <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">    <span class="keyword">if</span>(x &gt; y)</span><br><span class="line">        <span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line">    <span class="keyword">return</span> <span class="number">-1</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h4 id="2-2-2-2-解决方案-2-化浮为整"><a href="#2-2-2-2-解决方案-2-化浮为整" class="headerlink" title="2.2.2.2 解决方案 2 化浮为整"></a>2.2.2.2 解决方案 2 化浮为整</h4><p>在不溢出整数范围的情况下，可以通过乘上$10 ^ k$转化为整数运算，最后再将结果转化为浮点数输出</p>
<h4 id="2-2-3-负零"><a href="#2-2-3-负零" class="headerlink" title="2.2.3 负零"></a>2.2.3 负零</h4><p>输出时一定要小心不要输出 $-0$，比如</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">double</span> a = <span class="number">-0.000001</span>;</span><br><span class="line"><span class="built_in">printf</span>(<span class="string">"%.4f"</span>, a);</span><br></pre></td></tr></table></figure>
<h4 id="2-2-4-反三角函数"><a href="#2-2-4-反三角函数" class="headerlink" title="2.2.4 反三角函数"></a>2.2.4 反三角函数</h4><p>使用反三角函数时，要注意定义域的范围，比如，经过计算 x = 1.000001</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">double</span> x = <span class="number">1.000001</span>;</span><br><span class="line"><span class="keyword">double</span> acx = <span class="built_in">acos</span>(x);</span><br><span class="line"><span class="comment">//可能会返回runtime error</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//此时我们可以加一句判断</span></span><br><span class="line"><span class="keyword">double</span> x = <span class="number">1.000001</span>;</span><br><span class="line"><span class="keyword">if</span>(<span class="built_in">fabs</span>(x - <span class="number">1.0</span>) &lt; eps || <span class="built_in">fabs</span>(x + <span class="number">1.0</span>) &lt; eps)</span><br><span class="line">    x = round(x);</span><br><span class="line"><span class="keyword">double</span> acx = <span class="built_in">acos</span>(x);</span><br></pre></td></tr></table></figure>
<h3 id="2-3-模板总结"><a href="#2-3-模板总结" class="headerlink" title="2.3 模板总结"></a>2.3 模板总结</h3><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;cstdio&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;cmath&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> <span class="keyword">double</span> pi = <span class="built_in">acos</span>(<span class="number">-1.0</span>);</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">double</span> inf = <span class="number">1e100</span>;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">double</span> eps = <span class="number">1e-6</span>;</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">sgn</span><span class="params">(<span class="keyword">double</span> d)</span></span>&#123;</span><br><span class="line">    <span class="keyword">if</span>(<span class="built_in">fabs</span>(d) &lt; eps)</span><br><span class="line">        <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">    <span class="keyword">if</span>(d &gt; <span class="number">0</span>)</span><br><span class="line">        <span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line">    <span class="keyword">return</span> <span class="number">-1</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">dcmp</span><span class="params">(<span class="keyword">double</span> x, <span class="keyword">double</span> y)</span></span>&#123;</span><br><span class="line">    <span class="keyword">if</span>(<span class="built_in">fabs</span>(x - y) &lt; eps)</span><br><span class="line">        <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">    <span class="keyword">if</span>(x &gt; y)</span><br><span class="line">        <span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line">    <span class="keyword">return</span> <span class="number">-1</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    <span class="keyword">double</span> x = <span class="number">1.49999</span>;</span><br><span class="line">    <span class="keyword">int</span> fx = <span class="built_in">floor</span>(x);<span class="comment">//向下取整函数</span></span><br><span class="line">    <span class="keyword">int</span> cx = <span class="built_in">ceil</span>(x);<span class="comment">//向上取整函数</span></span><br><span class="line">    <span class="keyword">int</span> rx = round(x);<span class="comment">//四舍五入函数</span></span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">"%f %d %d %d\n"</span>, x, fx, cx, rx);</span><br><span class="line">    <span class="comment">//输出结果 1.499990 1 2 1</span></span><br><span class="line">    <span class="keyword">return</span>  <span class="number">0</span> ;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="3-点与向量"><a href="#3-点与向量" class="headerlink" title="3 点与向量"></a>3 点与向量</h2><h3 id="3-2-相关定义"><a href="#3-2-相关定义" class="headerlink" title="3.2 相关定义"></a>3.2 相关定义</h3><h4 id="3-1-1-点的定义"><a href="#3-1-1-点的定义" class="headerlink" title="3.1.1 点的定义"></a>3.1.1 点的定义</h4><p>二维平面下的点的表示只需要两个实数，即横坐标与纵坐标即可</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Point</span>&#123;</span></span><br><span class="line">    <span class="keyword">double</span> x, y;</span><br><span class="line">    Point(<span class="keyword">double</span> x = <span class="number">0</span>, <span class="keyword">double</span> y = <span class="number">0</span>):x(x),y(y)&#123;&#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<h4 id="3-1-2-向量的定义"><a href="#3-1-2-向量的定义" class="headerlink" title="3.1.2 向量的定义"></a>3.1.2 向量的定义</h4><ul>
<li>既有大小又有方向的量叫做向量</li>
<li>在计算机中我们常用坐标表示</li>
</ul>
<p>这样看来，向量这个结构体貌似与点没有任何区别，因此我们可以</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">typedef</span> Point Vector;</span><br></pre></td></tr></table></figure>
<h3 id="3-2-运算"><a href="#3-2-运算" class="headerlink" title="3.2 运算"></a>3.2 运算</h3><h4 id="3-2-1-加减乘除"><a href="#3-2-1-加减乘除" class="headerlink" title="3.2.1 加减乘除"></a>3.2.1 加减乘除</h4><h5 id="3-2-1-1-加法运算"><a href="#3-2-1-1-加法运算" class="headerlink" title="3.2.1.1 加法运算"></a>3.2.1.1 加法运算</h5><ul>
<li><p>点与点之间的加法运算没有意义</p>
</li>
<li><p>点与向量相加得到另一个点</p>
</li>
<li><p>向量与向量相加得到另外一个向量</p>
</li>
</ul>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">Vector <span class="keyword">operator</span> + (Vector A, Vector B)&#123;</span><br><span class="line">    <span class="keyword">return</span> Vector(A.x+B.x, A.y+B.y);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h5 id="3-2-1-2-减法运算"><a href="#3-2-1-2-减法运算" class="headerlink" title="3.2.1.2 减法运算"></a>3.2.1.2 减法运算</h5><p>两个点之间作差将得到一个向量，$A - B$将得到由$B$指向$A$的向量$\vec{BA}$</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">Vector <span class="keyword">operator</span> - (Point A, Point B)&#123;</span><br><span class="line">    <span class="keyword">return</span> Vector(A.x-B.x, A.y-B.y);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h5 id="3-2-1-3-乘法运算"><a href="#3-2-1-3-乘法运算" class="headerlink" title="3.2.1.3 乘法运算"></a>3.2.1.3 乘法运算</h5><p>向量与实数相乘得到等比例缩放的向量</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">Vector <span class="keyword">operator</span> * (Vector A, <span class="keyword">double</span> p)&#123;</span><br><span class="line">    <span class="keyword">return</span> Vector(A.x*p, A.y*p);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h5 id="3-2-1-4-除法运算"><a href="#3-2-1-4-除法运算" class="headerlink" title="3.2.1.4 除法运算"></a>3.2.1.4 除法运算</h5><p>向量与实数相除得到等比例缩放的向量</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">Vector <span class="keyword">operator</span> / (Vector A, <span class="keyword">double</span> p)&#123;</span><br><span class="line">    <span class="keyword">return</span> Vector(A.x/p, A.y/p);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h4 id="3-2-2-小于运算（Left-Then-Low-排序）"><a href="#3-2-2-小于运算（Left-Then-Low-排序）" class="headerlink" title="3.2.2 小于运算（Left Then Low 排序）"></a>3.2.2 小于运算（Left Then Low 排序）</h4><p>有时我们需要将点集按照$x$坐标升序排列，若$x$坐标相同，则按照$y$坐标升序排列</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">bool</span> <span class="keyword">operator</span> &lt; (<span class="keyword">const</span> Point&amp; a, <span class="keyword">const</span> Point&amp; b)&#123;</span><br><span class="line">    <span class="keyword">if</span>(a.x == b.x)</span><br><span class="line">        <span class="keyword">return</span> a.y &lt; b.y;</span><br><span class="line">    <span class="keyword">return</span> a.x &lt; b.x;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>此比较器将在Andrew算法中用到</p>
<p>而Graham Scan算法用到的比较器基于极角排序</p>
<h4 id="3-2-3-等于运算"><a href="#3-2-3-等于运算" class="headerlink" title="3.2.3 等于运算"></a>3.2.3 等于运算</h4><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">bool</span> <span class="keyword">operator</span> == (<span class="keyword">const</span> Point&amp; a, <span class="keyword">const</span> Point&amp; b)&#123;</span><br><span class="line">    <span class="keyword">if</span>(dcmp(a.x-b.x) == <span class="number">0</span> &amp;&amp; dcmp(a.y-b.y) == <span class="number">0</span>)</span><br><span class="line">        <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">    <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h4 id="3-2-4-内积运算"><a href="#3-2-4-内积运算" class="headerlink" title="3.2.4  内积运算"></a>3.2.4  内积运算</h4><p>又称数量积，点积</p>
<p>$\alpha \cdot \beta = |\alpha||\beta|cos\theta$</p>
<p>对加法满足分配律</p>
<h5 id="3-2-4-1-几何意义"><a href="#3-2-4-1-几何意义" class="headerlink" title="3.2.4.1 几何意义"></a>3.2.4.1 几何意义</h5><p>向量$\alpha$在向量$\beta$的投影$\alpha ’$（带有方向性）与$\beta$的长度乘积</p>
<ul>
<li><p>若$\alpha$与$\beta$的夹角为锐角，则其内积为正</p>
</li>
<li><p>若$\alpha$与$\beta$的夹角为钝角，则其内积为负</p>
</li>
<li><p>若$\alpha$与$\beta$的夹角为直角，则其内积为0</p>
</li>
</ul>
<h5 id="3-2-4-2-代码实现"><a href="#3-2-4-2-代码实现" class="headerlink" title="3.2.4.2 代码实现"></a>3.2.4.2 代码实现</h5><p>常用的实现方法有重载*运算符，或是单独写成函数，下面给出后一种实现方式</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">double</span> <span class="title">Dot</span><span class="params">(Vector A, Vector B)</span></span>&#123;</span><br><span class="line">    <span class="keyword">return</span> A.x*B.x + A.y*B.y;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h4 id="3-2-5-外积运算"><a href="#3-2-5-外积运算" class="headerlink" title="3.2.5 外积运算"></a>3.2.5 外积运算</h4><p>又称向量积，叉积</p>
<p>$\alpha \times \beta = |\alpha||\beta|sin\theta$</p>
<p>$\theta$表示向量$\alpha$旋转到向量$\beta$所经过的夹角</p>
<p>对加法满足分配律</p>
<h5 id="3-2-5-1-几何意义"><a href="#3-2-5-1-几何意义" class="headerlink" title="3.2.5.1 几何意义"></a>3.2.5.1 几何意义</h5><p>向量$\alpha$与$\beta$所张成的平行四边形的有向面积</p>
<h5 id="3-2-5-2-判断外积的符号"><a href="#3-2-5-2-判断外积的符号" class="headerlink" title="3.2.5.2 判断外积的符号"></a>3.2.5.2 判断外积的符号</h5><p>右手定则</p>
<p>$\alpha \times \beta$</p>
<p>若$\beta$在$\alpha$的逆时针方向，则为正值</p>
<p>顺时针则为负值</p>
<p>两向量共线则为0</p>
<h5 id="3-2-5-3-代码实现"><a href="#3-2-5-3-代码实现" class="headerlink" title="3.2.5.3 代码实现"></a>3.2.5.3 代码实现</h5><p>重载^运算符或者单独写成函数</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">double</span> <span class="title">Cross</span><span class="params">(Vector A, Vector B)</span></span>&#123;</span><br><span class="line">    <span class="keyword">return</span> A.x*B.y-A.y*B.x;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="3-3-常用函数"><a href="#3-3-常用函数" class="headerlink" title="3.3 常用函数"></a>3.3 常用函数</h3><h4 id="3-3-1-取模（长度）"><a href="#3-3-1-取模（长度）" class="headerlink" title="3.3.1 取模（长度）"></a>3.3.1 取模（长度）</h4><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">double</span> <span class="title">Length</span><span class="params">(Vector A)</span></span>&#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="built_in">sqrt</span>(Dot(A, A));</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h4 id="3-3-2-计算两向量夹角"><a href="#3-3-2-计算两向量夹角" class="headerlink" title="3.3.2 计算两向量夹角"></a>3.3.2 计算两向量夹角</h4><p>返回值为弧度制下的夹角</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">double</span> <span class="title">Angle</span><span class="params">(Vector A, Vector B)</span></span>&#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="built_in">acos</span>(Dot(A, B) / Length(A) / Length(B));</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h4 id="3-3-3-计算两向量构成的平行四边形有向面积"><a href="#3-3-3-计算两向量构成的平行四边形有向面积" class="headerlink" title="3.3.3 计算两向量构成的平行四边形有向面积"></a>3.3.3 计算两向量构成的平行四边形有向面积</h4><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">double</span> <span class="title">Area2</span><span class="params">(Point A, Point B, Point C)</span></span>&#123;</span><br><span class="line">    <span class="keyword">return</span> Cross(B-A, C-A);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h4 id="3-3-4-计算向量逆时针旋转后的向量"><a href="#3-3-4-计算向量逆时针旋转后的向量" class="headerlink" title="3.3.4 计算向量逆时针旋转后的向量"></a>3.3.4 计算向量逆时针旋转后的向量</h4><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="function">Vector <span class="title">Rotate</span><span class="params">(Vector A, <span class="keyword">double</span> rad)</span></span>&#123;<span class="comment">//rad为弧度 且为逆时针旋转的角</span></span><br><span class="line">    <span class="keyword">return</span> Vector(A.x*<span class="built_in">cos</span>(rad)-A.y*<span class="built_in">sin</span>(rad), A.x*<span class="built_in">sin</span>(rad)+A.y*<span class="built_in">cos</span>(rad));</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h4 id="3-3-5-计算向量逆时针旋转九十度的单位法向量"><a href="#3-3-5-计算向量逆时针旋转九十度的单位法向量" class="headerlink" title="3.3.5 计算向量逆时针旋转九十度的单位法向量"></a>3.3.5 计算向量逆时针旋转九十度的单位法向量</h4><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="function">Vector <span class="title">Normal</span><span class="params">(Vector A)</span></span>&#123;<span class="comment">//向量A左转90°的单位法向量</span></span><br><span class="line">    <span class="keyword">double</span> L = Length(A);</span><br><span class="line">    <span class="keyword">return</span> Vector(-A.y/L, A.x/L);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h4 id="3-3-6-ToLeftTest"><a href="#3-3-6-ToLeftTest" class="headerlink" title="3.3.6 ToLeftTest"></a>3.3.6 ToLeftTest</h4><p>判断折线$\vec{bc}$是不是向$\vec{ab}$的逆时针方向（左边）转向</p>
<p>凸包构造时将会频繁用到此公式</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">bool</span> <span class="title">ToLeftTest</span><span class="params">(Point a, Point b, Point c)</span></span>&#123;</span><br><span class="line">    <span class="keyword">return</span> Cross(b - a, c - b) &gt; <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="3-4-复数黑科技"><a href="#3-4-复数黑科技" class="headerlink" title="3.4 复数黑科技"></a>3.4 复数黑科技</h3><p>利用复数黑科技实现平面点与向量</p>
<p>复数定义向量后，自动拥有构造函数、加减法和数量积</p>
<h4 id="3-4-1-代码实现"><a href="#3-4-1-代码实现" class="headerlink" title="3.4.1 代码实现"></a>3.4.1 代码实现</h4><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;complex&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"><span class="keyword">typedef</span> <span class="keyword">complex</span>&lt;<span class="keyword">double</span>&gt; Point;</span><br><span class="line"><span class="keyword">typedef</span> Point Vector;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">double</span> eps = <span class="number">1e-9</span>;</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">dcmp</span><span class="params">(<span class="keyword">double</span> x)</span></span>&#123;</span><br><span class="line">    <span class="keyword">if</span>(<span class="built_in">fabs</span>(x) &lt; eps)</span><br><span class="line">        <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">    <span class="keyword">if</span>(x &lt; <span class="number">0</span>)</span><br><span class="line">        <span class="keyword">return</span> <span class="number">-1</span>;</span><br><span class="line">    <span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">double</span> <span class="title">Length</span><span class="params">(Vector A)</span></span>&#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="built_in">abs</span>(A);</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">double</span> <span class="title">Dot</span><span class="params">(Vector A, Vector B)</span></span>&#123;<span class="comment">//conj(a+bi)返回共轭复数a-bi</span></span><br><span class="line">    <span class="keyword">return</span> real(conj(A)*B);</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">double</span> <span class="title">Cross</span><span class="params">(Vector A, Vector B)</span></span>&#123;</span><br><span class="line">    <span class="keyword">return</span> imag(conj(A)*B);</span><br><span class="line">&#125;</span><br><span class="line"><span class="function">Vector <span class="title">Rotate</span><span class="params">(Vector A, <span class="keyword">double</span> rad)</span></span>&#123;</span><br><span class="line">    <span class="keyword">return</span> A*<span class="built_in">exp</span>(Point(<span class="number">0</span>, rad));<span class="comment">//exp(p)返回以e为底复数的指数</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h4 id="3-4-2-不足"><a href="#3-4-2-不足" class="headerlink" title="3.4.2 不足"></a>3.4.2 不足</h4><p>复数运算会比自己写的向量运算慢，若题目时间要求比较苛刻，要谨慎使用</p>
<h3 id="3-5-模板总结"><a href="#3-5-模板总结" class="headerlink" title="3.5 模板总结"></a>3.5 模板总结</h3><h4 id="3-5-1-手动实现"><a href="#3-5-1-手动实现" class="headerlink" title="3.5.1 手动实现"></a>3.5.1 手动实现</h4><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Point</span>&#123;</span></span><br><span class="line">    <span class="keyword">double</span> x, y;</span><br><span class="line">    Point(<span class="keyword">double</span> x = <span class="number">0</span>, <span class="keyword">double</span> y = <span class="number">0</span>):x(x),y(y)&#123;&#125;</span><br><span class="line">&#125;;</span><br><span class="line"><span class="keyword">typedef</span> Point Vector;</span><br><span class="line">Vector <span class="keyword">operator</span> + (Vector A, Vector B)&#123;</span><br><span class="line">    <span class="keyword">return</span> Vector(A.x+B.x, A.y+B.y);</span><br><span class="line">&#125;</span><br><span class="line">Vector <span class="keyword">operator</span> - (Point A, Point B)&#123;</span><br><span class="line">    <span class="keyword">return</span> Vector(A.x-B.x, A.y-B.y);</span><br><span class="line">&#125;</span><br><span class="line">Vector <span class="keyword">operator</span> * (Vector A, <span class="keyword">double</span> p)&#123;</span><br><span class="line">    <span class="keyword">return</span> Vector(A.x*p, A.y*p);</span><br><span class="line">&#125;</span><br><span class="line">Vector <span class="keyword">operator</span> / (Vector A, <span class="keyword">double</span> p)&#123;</span><br><span class="line">    <span class="keyword">return</span> Vector(A.x/p, A.y/p);</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">bool</span> <span class="keyword">operator</span> &lt; (<span class="keyword">const</span> Point&amp; a, <span class="keyword">const</span> Point&amp; b)&#123;</span><br><span class="line">    <span class="keyword">if</span>(a.x == b.x)</span><br><span class="line">        <span class="keyword">return</span> a.y &lt; b.y;</span><br><span class="line">    <span class="keyword">return</span> a.x &lt; b.x;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">double</span> eps = <span class="number">1e-6</span>;</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">sgn</span><span class="params">(<span class="keyword">double</span> x)</span></span>&#123;</span><br><span class="line">    <span class="keyword">if</span>(<span class="built_in">fabs</span>(x) &lt; eps)</span><br><span class="line">        <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">    <span class="keyword">if</span>(x &lt; <span class="number">0</span>)</span><br><span class="line">        <span class="keyword">return</span> <span class="number">-1</span>;</span><br><span class="line">    <span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">bool</span> <span class="keyword">operator</span> == (<span class="keyword">const</span> Point&amp; a, <span class="keyword">const</span> Point&amp; b)&#123;</span><br><span class="line">    <span class="keyword">if</span>(sgn(a.x-b.x) == <span class="number">0</span> &amp;&amp; sgn(a.y-b.y) == <span class="number">0</span>)</span><br><span class="line">        <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">    <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">double</span> <span class="title">Dot</span><span class="params">(Vector A, Vector B)</span></span>&#123;</span><br><span class="line">    <span class="keyword">return</span> A.x*B.x + A.y*B.y;</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">double</span> <span class="title">Length</span><span class="params">(Vector A)</span></span>&#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="built_in">sqrt</span>(Dot(A, A));</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">double</span> <span class="title">Angle</span><span class="params">(Vector A, Vector B)</span></span>&#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="built_in">acos</span>(Dot(A, B)/Length(A)/Length(B));</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">double</span> <span class="title">Cross</span><span class="params">(Vector A, Vector B)</span></span>&#123;</span><br><span class="line">    <span class="keyword">return</span> A.x*B.y-A.y*B.x;</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">double</span> <span class="title">Area2</span><span class="params">(Point A, Point B, Point C)</span></span>&#123;</span><br><span class="line">    <span class="keyword">return</span> Cross(B-A, C-A);</span><br><span class="line">&#125;</span><br><span class="line"><span class="function">Vector <span class="title">Rotate</span><span class="params">(Vector A, <span class="keyword">double</span> rad)</span></span>&#123;<span class="comment">//rad为弧度 且为逆时针旋转的角</span></span><br><span class="line">    <span class="keyword">return</span> Vector(A.x*<span class="built_in">cos</span>(rad)-A.y*<span class="built_in">sin</span>(rad), A.x*<span class="built_in">sin</span>(rad)+A.y*<span class="built_in">cos</span>(rad));</span><br><span class="line">&#125;</span><br><span class="line"><span class="function">Vector <span class="title">Normal</span><span class="params">(Vector A)</span></span>&#123;<span class="comment">//向量A左转90°的单位法向量</span></span><br><span class="line">    <span class="keyword">double</span> L = Length(A);</span><br><span class="line">    <span class="keyword">return</span> Vector(-A.y/L, A.x/L);</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">bool</span> <span class="title">ToLeftTest</span><span class="params">(Point a, Point b, Point c)</span></span>&#123;</span><br><span class="line">    <span class="keyword">return</span> Cross(b - a, c - b) &gt; <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h4 id="3-5-2-复数黑科技"><a href="#3-5-2-复数黑科技" class="headerlink" title="3.5.2 复数黑科技"></a>3.5.2 复数黑科技</h4><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;complex&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"><span class="keyword">typedef</span> <span class="keyword">complex</span>&lt;<span class="keyword">double</span>&gt; Point;</span><br><span class="line"><span class="keyword">typedef</span> Point Vector;<span class="comment">//复数定义向量后，自动拥有构造函数、加减法和数量积</span></span><br><span class="line"><span class="keyword">const</span> <span class="keyword">double</span> eps = <span class="number">1e-9</span>;</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">sgn</span><span class="params">(<span class="keyword">double</span> x)</span></span>&#123;</span><br><span class="line">    <span class="keyword">if</span>(<span class="built_in">fabs</span>(x) &lt; eps)</span><br><span class="line">        <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">    <span class="keyword">if</span>(x &lt; <span class="number">0</span>)</span><br><span class="line">        <span class="keyword">return</span> <span class="number">-1</span>;</span><br><span class="line">    <span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">double</span> <span class="title">Length</span><span class="params">(Vector A)</span></span>&#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="built_in">abs</span>(A);</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">double</span> <span class="title">Dot</span><span class="params">(Vector A, Vector B)</span></span>&#123;<span class="comment">//conj(a+bi)返回共轭复数a-bi</span></span><br><span class="line">    <span class="keyword">return</span> real(conj(A)*B);</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">double</span> <span class="title">Cross</span><span class="params">(Vector A, Vector B)</span></span>&#123;</span><br><span class="line">    <span class="keyword">return</span> imag(conj(A)*B);</span><br><span class="line">&#125;</span><br><span class="line"><span class="function">Vector <span class="title">Rotate</span><span class="params">(Vector A, <span class="keyword">double</span> rad)</span></span>&#123;</span><br><span class="line">    <span class="keyword">return</span> A*<span class="built_in">exp</span>(Point(<span class="number">0</span>, rad));<span class="comment">//exp(p)返回以e为底复数的指数</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="4-点与线"><a href="#4-点与线" class="headerlink" title="4 点与线"></a>4 点与线</h2><h3 id="4-1-定义"><a href="#4-1-定义" class="headerlink" title="4.1 定义"></a>4.1 定义</h3><h4 id="4-1-1-直线定义"><a href="#4-1-1-直线定义" class="headerlink" title="4.1.1 直线定义"></a>4.1.1 直线定义</h4><p>直线表示常用的有三种形式</p>
<ul>
<li><p>一般式$ax + by + c = 0$</p>
</li>
<li><p>点向式$x_0 + y_0 + v_xt  + v_yt = 0$</p>
</li>
<li><p>斜截式$y = kx + b$</p>
</li>
</ul>
<p>计算机中常用点向式表示直线，即参数方程形式表示</p>
<h4 id="4-1-2-点向式"><a href="#4-1-2-点向式" class="headerlink" title="4.1.2 点向式"></a>4.1.2 点向式</h4><p>直线可以用直线上的一个点$P_0$和方向向量$v$表示</p>
<script type="math/tex; mode=display">P = P _ 0 + vt</script><p>其中$t$为参数</p>
<h5 id="4-1-2-1-优点"><a href="#4-1-2-1-优点" class="headerlink" title="4.1.2.1 优点"></a>4.1.2.1 优点</h5><ul>
<li><p>可以表示所有直线</p>
</li>
<li><p>可以通过限制参数来表示线段和射线</p>
</li>
</ul>
<h4 id="4-1-3-线段与射线"><a href="#4-1-3-线段与射线" class="headerlink" title="4.1.3 线段与射线"></a>4.1.3 线段与射线</h4><p>利用带参数限制的直线点向式方程表示</p>
<h3 id="4-2-实现"><a href="#4-2-实现" class="headerlink" title="4.2 实现"></a>4.2 实现</h3><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Line</span>&#123;</span><span class="comment">//直线定义</span></span><br><span class="line">    Point v, p;</span><br><span class="line">    Line(Point v, Point p):v(v), p(p) &#123;&#125;</span><br><span class="line">    <span class="function">Point <span class="title">point</span><span class="params">(<span class="keyword">double</span> t)</span></span>&#123;<span class="comment">//返回点P = v + (p - v)*t</span></span><br><span class="line">        <span class="keyword">return</span> v + (p - v)*t;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<h3 id="4-3-常用操作"><a href="#4-3-常用操作" class="headerlink" title="4.3 常用操作"></a>4.3 常用操作</h3><h4 id="4-3-1-判断点在直线上"><a href="#4-3-1-判断点在直线上" class="headerlink" title="4.3.1 判断点在直线上"></a>4.3.1 判断点在直线上</h4><ul>
<li><p>利用三点共线的等价条件$\alpha \times \beta == 0$</p>
</li>
<li><p>直线上取两不同点与待测点构成向量求叉积是否为零</p>
</li>
</ul>
<h4 id="4-3-2-计算两直线交点"><a href="#4-3-2-计算两直线交点" class="headerlink" title="4.3.2 计算两直线交点"></a>4.3.2 计算两直线交点</h4><p>必须保证直线相交，否则将会出现除以零的情况</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//调用前需保证 Cross(v, w) != 0</span></span><br><span class="line"><span class="function">Point <span class="title">GetLineIntersection</span><span class="params">(Point P, Vector v, Point Q, Vector w)</span></span>&#123;</span><br><span class="line">    Vector u = P-Q;</span><br><span class="line">    <span class="keyword">double</span> t = Cross(w, u)/Cross(v, w);</span><br><span class="line">    <span class="keyword">return</span> P+v*t;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h4 id="4-3-3-计算点到直线的距离"><a href="#4-3-3-计算点到直线的距离" class="headerlink" title="4.3.3 计算点到直线的距离"></a>4.3.3 计算点到直线的距离</h4><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//点P到直线AB距离公式</span></span><br><span class="line"><span class="function"><span class="keyword">double</span> <span class="title">DistanceToLine</span><span class="params">(Point P, Point A, Point B)</span></span>&#123;</span><br><span class="line">    Vector v1 = B-A, v2 = P-A;</span><br><span class="line">    <span class="keyword">return</span> <span class="built_in">fabs</span>(Cross(v1, v2)/Length(v1));</span><br><span class="line">&#125;<span class="comment">//不去绝对值，得到的是有向距离</span></span><br></pre></td></tr></table></figure>
<h4 id="4-3-4-计算点到线段的距离"><a href="#4-3-4-计算点到线段的距离" class="headerlink" title="4.3.4 计算点到线段的距离"></a>4.3.4 计算点到线段的距离</h4><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//点P到线段AB距离公式</span></span><br><span class="line"><span class="function"><span class="keyword">double</span> <span class="title">DistanceToSegment</span><span class="params">(Point P, Point A, Point B)</span></span>&#123;</span><br><span class="line">    <span class="keyword">if</span>(A == B)</span><br><span class="line">        <span class="keyword">return</span> Length(P-A);</span><br><span class="line">    Vector v1 = B-A, v2 = P-A, v3 = P-B;</span><br><span class="line">    <span class="keyword">if</span>(dcmp(Dot(v1, v2)) &lt; <span class="number">0</span>)</span><br><span class="line">        <span class="keyword">return</span> Length(v2);</span><br><span class="line">    <span class="keyword">if</span>(dcmp(Dot(v1, v3)) &gt; <span class="number">0</span>)</span><br><span class="line">        <span class="keyword">return</span> Length(v3);</span><br><span class="line">    <span class="keyword">return</span> DistanceToLine(P, A, B);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h4 id="4-3-5-求点在直线上的投影点"><a href="#4-3-5-求点在直线上的投影点" class="headerlink" title="4.3.5 求点在直线上的投影点"></a>4.3.5 求点在直线上的投影点</h4><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//点P在直线AB上的投影点</span></span><br><span class="line"><span class="function">Point <span class="title">GetLineProjection</span><span class="params">(Point P, Point A, Point B)</span></span>&#123;</span><br><span class="line">    Vector v = B-A;</span><br><span class="line">    <span class="keyword">return</span> A+v*(Dot(v, P-A)/Dot(v, v));</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h4 id="4-3-6-判断点是否在线段上"><a href="#4-3-6-判断点是否在线段上" class="headerlink" title="4.3.6 判断点是否在线段上"></a>4.3.6 判断点是否在线段上</h4><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">bool</span> <span class="title">OnSegment</span><span class="params">(Point p, Point a1, Point a2)</span></span>&#123;</span><br><span class="line">    <span class="keyword">return</span> dcmp(Cross(a1-p, a2-p)) == <span class="number">0</span> &amp;&amp; dcmp(Dot(a1-p, a2-p)) &lt; <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h4 id="4-3-7-判断两线段是否相交"><a href="#4-3-7-判断两线段是否相交" class="headerlink" title="4.3.7 判断两线段是否相交"></a>4.3.7 判断两线段是否相交</h4><p>通过两次跨立实验</p>
<p>不允许在顶点处相交</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">bool</span> <span class="title">SegmentProperIntersection</span><span class="params">(Point a1, Point a2, Point b1, Point b2)</span></span>&#123;</span><br><span class="line">    <span class="keyword">double</span> c1 = Cross(a2 - a1, b1 - a1), c2 = Cross(a2 - a1, b2 - a1);</span><br><span class="line">    <span class="keyword">double</span> c3 = Cross(b2 - b1, a1 - b1), c4 = Cross(b2 - b1, a2 - b1);</span><br><span class="line">    <span class="keyword">return</span> (sgn(c1)*sgn(c2) &lt; <span class="number">0</span> &amp;&amp; sgn(c3)*sgn(c4) &lt; <span class="number">0</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>允许在端点处相交</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">bool</span> <span class="title">SegmentProperIntersection</span><span class="params">(Point a1, Point a2, Point b1, Point b2)</span></span>&#123;</span><br><span class="line">    <span class="keyword">double</span> c1 = Cross(a2-a1, b1-a1), c2 = Cross(a2-a1, b2-a1);</span><br><span class="line">    <span class="keyword">double</span> c3 = Cross(b2-b1, a1-b1), c4 = Cross(b2-b1, a2-b1);</span><br><span class="line">    <span class="comment">//if判断控制是否允许线段在端点处相交，根据需要添加</span></span><br><span class="line">    <span class="keyword">if</span>(!sgn(c1) || !sgn(c2) || !sgn(c3) || !sgn(c4))&#123;</span><br><span class="line">        <span class="keyword">bool</span> f1 = OnSegment(b1, a1, a2);</span><br><span class="line">        <span class="keyword">bool</span> f2 = OnSegment(b2, a1, a2);</span><br><span class="line">        <span class="keyword">bool</span> f3 = OnSegment(a1, b1, b2);</span><br><span class="line">        <span class="keyword">bool</span> f4 = OnSegment(a2, b1, b2);</span><br><span class="line">        <span class="keyword">bool</span> f = (f1|f2|f3|f4);</span><br><span class="line">        <span class="keyword">return</span> f;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> (sgn(c1)*sgn(c2) &lt; <span class="number">0</span> &amp;&amp; sgn(c3)*sgn(c4) &lt; <span class="number">0</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="4-4-模板总结"><a href="#4-4-模板总结" class="headerlink" title="4.4 模板总结"></a>4.4 模板总结</h3><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Line</span>&#123;</span><span class="comment">//直线定义</span></span><br><span class="line">    Point v, p;</span><br><span class="line">    Line(Point v, Point p):v(v), p(p) &#123;&#125;</span><br><span class="line">    <span class="function">Point <span class="title">point</span><span class="params">(<span class="keyword">double</span> t)</span></span>&#123;<span class="comment">//返回点P = v + (p - v)*t</span></span><br><span class="line">        <span class="keyword">return</span> v + (p - v)*t;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br><span class="line"><span class="comment">//计算两直线交点</span></span><br><span class="line"><span class="comment">//调用前需保证 Cross(v, w) != 0</span></span><br><span class="line"><span class="function">Point <span class="title">GetLineIntersection</span><span class="params">(Point P, Vector v, Point Q, Vector w)</span></span>&#123;</span><br><span class="line">    Vector u = P-Q;</span><br><span class="line">    <span class="keyword">double</span> t = Cross(w, u)/Cross(v, w);</span><br><span class="line">    <span class="keyword">return</span> P+v*t;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//点P到直线AB距离公式</span></span><br><span class="line"><span class="function"><span class="keyword">double</span> <span class="title">DistanceToLine</span><span class="params">(Point P, Point A, Point B)</span></span>&#123;</span><br><span class="line">    Vector v1 = B-A, v2 = P-A;</span><br><span class="line">    <span class="keyword">return</span> <span class="built_in">fabs</span>(Cross(v1, v2)/Length(v1));</span><br><span class="line">&#125;<span class="comment">//不去绝对值，得到的是有向距离</span></span><br><span class="line"><span class="comment">//点P到线段AB距离公式</span></span><br><span class="line"><span class="function"><span class="keyword">double</span> <span class="title">DistanceToSegment</span><span class="params">(Point P, Point A, Point B)</span></span>&#123;</span><br><span class="line">    <span class="keyword">if</span>(A == B)</span><br><span class="line">        <span class="keyword">return</span> Length(P-A);</span><br><span class="line">    Vector v1 = B-A, v2 = P-A, v3 = P-B;</span><br><span class="line">    <span class="keyword">if</span>(dcmp(Dot(v1, v2)) &lt; <span class="number">0</span>)</span><br><span class="line">        <span class="keyword">return</span> Length(v2);</span><br><span class="line">    <span class="keyword">if</span>(dcmp(Dot(v1, v3)) &gt; <span class="number">0</span>)</span><br><span class="line">        <span class="keyword">return</span> Length(v3);</span><br><span class="line">    <span class="keyword">return</span> DistanceToLine(P, A, B);</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//点P在直线AB上的投影点</span></span><br><span class="line"><span class="function">Point <span class="title">GetLineProjection</span><span class="params">(Point P, Point A, Point B)</span></span>&#123;</span><br><span class="line">    Vector v = B-A;</span><br><span class="line">    <span class="keyword">return</span> A+v*(Dot(v, P-A)/Dot(v, v));</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//判断p点是否在线段a1a2上</span></span><br><span class="line"><span class="function"><span class="keyword">bool</span> <span class="title">OnSegment</span><span class="params">(Point p, Point a1, Point a2)</span></span>&#123;</span><br><span class="line">    <span class="keyword">return</span> dcmp(Cross(a1-p, a2-p)) == <span class="number">0</span> &amp;&amp; dcmp(Dot(a1-p, a2-p)) &lt; <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//判断两线段是否相交</span></span><br><span class="line"><span class="function"><span class="keyword">bool</span> <span class="title">SegmentProperIntersection</span><span class="params">(Point a1, Point a2, Point b1, Point b2)</span></span>&#123;</span><br><span class="line">    <span class="keyword">double</span> c1 = Cross(a2-a1, b1-a1), c2 = Cross(a2-a1, b2-a1);</span><br><span class="line">    <span class="keyword">double</span> c3 = Cross(b2-b1, a1-b1), c4 = Cross(b2-b1, a2-b1);</span><br><span class="line">    <span class="comment">//if判断控制是否允许线段在端点处相交，根据需要添加</span></span><br><span class="line">    <span class="keyword">if</span>(!sgn(c1) || !sgn(c2) || !sgn(c3) || !sgn(c4))&#123;</span><br><span class="line">        <span class="keyword">bool</span> f1 = OnSegment(b1, a1, a2);</span><br><span class="line">        <span class="keyword">bool</span> f2 = OnSegment(b2, a1, a2);</span><br><span class="line">        <span class="keyword">bool</span> f3 = OnSegment(a1, b1, b2);</span><br><span class="line">        <span class="keyword">bool</span> f4 = OnSegment(a2, b1, b2);</span><br><span class="line">        <span class="keyword">bool</span> f = (f1|f2|f3|f4);</span><br><span class="line">        <span class="keyword">return</span> f;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> (sgn(c1)*sgn(c2) &lt; <span class="number">0</span> &amp;&amp; sgn(c3)*sgn(c4) &lt; <span class="number">0</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="5-多边形"><a href="#5-多边形" class="headerlink" title="5 多边形"></a>5 多边形</h2><h3 id="5-1-三角形"><a href="#5-1-三角形" class="headerlink" title="5.1 三角形"></a>5.1 三角形</h3><h4 id="5-1-1-三角形面积"><a href="#5-1-1-三角形面积" class="headerlink" title="5.1.1 三角形面积"></a>5.1.1 三角形面积</h4><ul>
<li><p>利用两条边叉积除以二取绝对值</p>
</li>
<li><p>海伦公式</p>
</li>
</ul>
<script type="math/tex; mode=display">S = \sqrt{p(p - a)(p - b)(p - c)}, p = \frac{(a + b + c)}{2}</script><ul>
<li>$S = \frac{absinC}{2}$</li>
</ul>
<h4 id="5-1-2-三角形四心"><a href="#5-1-2-三角形四心" class="headerlink" title="5.1.2 三角形四心"></a>5.1.2 三角形四心</h4><h5 id="5-1-2-1-外心"><a href="#5-1-2-1-外心" class="headerlink" title="5.1.2.1 外心"></a>5.1.2.1 外心</h5><p>三边中垂线交点，到三角形三个顶点距离相同</p>
<h5 id="5-1-2-2-内心"><a href="#5-1-2-2-内心" class="headerlink" title="5.1.2.2 内心"></a>5.1.2.2 内心</h5><p>角平分线的交点，到三角形三边的距离相同</p>
<h5 id="5-1-2-3-垂心"><a href="#5-1-2-3-垂心" class="headerlink" title="5.1.2.3 垂心"></a>5.1.2.3 垂心</h5><p>三条高线的交点</p>
<h5 id="5-1-2-4-重心"><a href="#5-1-2-4-重心" class="headerlink" title="5.1.2.4 重心"></a>5.1.2.4 重心</h5><p>三条中线的交点，到三角形三顶点距离的平方和最小的点，三角形内到三边距离之积最大的点</p>
<h3 id="5-2-普通多边形"><a href="#5-2-普通多边形" class="headerlink" title="5.2 普通多边形"></a>5.2 普通多边形</h3><p>通常按照逆时针储存所有顶点</p>
<h4 id="5-2-1-定义"><a href="#5-2-1-定义" class="headerlink" title="5.2.1 定义"></a>5.2.1 定义</h4><h5 id="5-2-1-1-多边形"><a href="#5-2-1-1-多边形" class="headerlink" title="5.2.1.1 多边形"></a>5.2.1.1 多边形</h5><p>由在同一平面且不再同一直线上的多条线段首位顺次连接且不相交所组成的图形交多边形</p>
<h5 id="5-2-1-2-简单多边形"><a href="#5-2-1-2-简单多边形" class="headerlink" title="5.2.1.2 简单多边形"></a>5.2.1.2 简单多边形</h5><p>简单多边形是除相邻边外其它边不相交的多边形</p>
<h5 id="5-2-1-3-凸多边形"><a href="#5-2-1-3-凸多边形" class="headerlink" title="5.2.1.3 凸多边形"></a>5.2.1.3 凸多边形</h5><p>过多边形的任意一边做一条直线，如果其他各个顶点都在这条直线的同侧，则把这个多边形叫做凸多边形</p>
<p>任意凸多边形外角和均为$360°$</p>
<p>任意凸多边形内角和为$(n - 2)180°$</p>
<h4 id="5-2-2-常用函数"><a href="#5-2-2-常用函数" class="headerlink" title="5.2.2 常用函数"></a>5.2.2 常用函数</h4><h5 id="5-2-2-1-求多边形面积"><a href="#5-2-2-1-求多边形面积" class="headerlink" title="5.2.2.1 求多边形面积"></a>5.2.2.1 求多边形面积</h5><p>我们可以从第一个顶点除法把凸多边形分成$n - 2$个三角形，然后把面积加起来</p>
<p>最后返回值说为有向面积更贴近本质</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">double</span> <span class="title">PolygonArea</span><span class="params">(Point* p, <span class="keyword">int</span> n)</span></span>&#123;<span class="comment">//p为端点集合，n为端点个数</span></span><br><span class="line">    <span class="keyword">double</span> s = <span class="number">0</span>;</span><br><span class="line">    <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">1</span>; i &lt; n<span class="number">-1</span>; ++i)</span><br><span class="line">        s += Cross(p[i]-p[<span class="number">0</span>], p[i+<span class="number">1</span>]-p[<span class="number">0</span>]);</span><br><span class="line">    <span class="keyword">return</span> s;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h5 id="5-2-2-2-判断点在多边形内"><a href="#5-2-2-2-判断点在多边形内" class="headerlink" title="5.2.2.2 判断点在多边形内"></a>5.2.2.2 判断点在多边形内</h5><p>有射线法与转角法。</p>
<p>转角法的基本思想是看多边形相对于这个点转了多少度</p>
<ul>
<li><p>如果是三百六十度，说明点在多边形内</p>
</li>
<li><p>如果是零度，说明点在多边形外</p>
</li>
<li><p>如果是一百八十度，说明点在多边形边界上</p>
</li>
</ul>
<p>如果直接按照定义来算，则需要计算大量反三角函数，不仅速度慢，而且容易产生精度问</p>
<p>我们采用winding number绕数来计算</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//判断点是否在多边形内，若点在多边形内返回1，在多边形外部返回0，在多边形上返回-1</span></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">isPointInPolygon</span><span class="params">(Point p, <span class="built_in">vector</span>&lt;Point&gt; poly)</span></span>&#123;</span><br><span class="line">    <span class="keyword">int</span> wn = <span class="number">0</span>;</span><br><span class="line">    <span class="keyword">int</span> n = poly.size();</span><br><span class="line">    <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; n; ++i)&#123;</span><br><span class="line">        <span class="keyword">if</span>(OnSegment(p, poly[i], poly[(i+<span class="number">1</span>)%n])) <span class="keyword">return</span> <span class="number">-1</span>;</span><br><span class="line">        <span class="keyword">int</span> k = sgn(Cross(poly[(i+<span class="number">1</span>)%n] - poly[i], p - poly[i]));</span><br><span class="line">        <span class="keyword">int</span> d1 = sgn(poly[i].y - p.y);</span><br><span class="line">        <span class="keyword">int</span> d2 = sgn(poly[(i+<span class="number">1</span>)%n].y - p.y);</span><br><span class="line">        <span class="keyword">if</span>(k &gt; <span class="number">0</span> &amp;&amp; d1 &lt;= <span class="number">0</span> &amp;&amp; d2 &gt; <span class="number">0</span>) wn++;</span><br><span class="line">        <span class="keyword">if</span>(k &lt; <span class="number">0</span> &amp;&amp; d2 &lt;= <span class="number">0</span> &amp;&amp; d1 &gt; <span class="number">0</span>) wn--;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">if</span>(wn != <span class="number">0</span>)</span><br><span class="line">        <span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h5 id="5-2-2-4-判断点在凸多边形内"><a href="#5-2-2-4-判断点在凸多边形内" class="headerlink" title="5.2.2.4 判断点在凸多边形内"></a>5.2.2.4 判断点在凸多边形内</h5><p>只需要判断点是否在所有边的左边（按逆时针顺序排列的顶点集）ToLeftTest</p>
<h3 id="5-3-Pick定理"><a href="#5-3-Pick定理" class="headerlink" title="5.3 Pick定理"></a>5.3 Pick定理</h3><h4 id="5-3-1-内容"><a href="#5-3-1-内容" class="headerlink" title="5.3.1 内容"></a>5.3.1 内容</h4><p>皮克定理是指一个计算点阵中顶点在格点上的多边形面积公式该公式可以表示为</p>
<script type="math/tex; mode=display">2S = 2a + b - 2</script><p>其中$a$表示多边形内部的点数，$b$表示多边形边界上的点数，$S$表示多边形的面积。</p>
<p>常用形式</p>
<script type="math/tex; mode=display">S = a + \frac{b}{2} - 1</script><h4 id="5-3-2-常用计算"><a href="#5-3-2-常用计算" class="headerlink" title="5.3.2 常用计算"></a>5.3.2 常用计算</h4><p>给你多边形的顶点，问多边形内部有多少点</p>
<p>$a = S - \frac{b}{2} + 1$</p>
<h3 id="5-4-模板总结"><a href="#5-4-模板总结" class="headerlink" title="5.4 模板总结"></a>5.4 模板总结</h3><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//多边形有向面积</span></span><br><span class="line"><span class="function"><span class="keyword">double</span> <span class="title">PolygonArea</span><span class="params">(Point* p, <span class="keyword">int</span> n)</span></span>&#123;<span class="comment">//p为端点集合，n为端点个数</span></span><br><span class="line">    <span class="keyword">double</span> s = <span class="number">0</span>;</span><br><span class="line">    <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">1</span>; i &lt; n<span class="number">-1</span>; ++i)</span><br><span class="line">        s += Cross(p[i]-p[<span class="number">0</span>], p[i+<span class="number">1</span>]-p[<span class="number">0</span>]);</span><br><span class="line">    <span class="keyword">return</span> s;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//判断点是否在多边形内，若点在多边形内返回1，在多边形外部返回0，在多边形上返回-1</span></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">isPointInPolygon</span><span class="params">(Point p, <span class="built_in">vector</span>&lt;Point&gt; poly)</span></span>&#123;</span><br><span class="line">    <span class="keyword">int</span> wn = <span class="number">0</span>;</span><br><span class="line">    <span class="keyword">int</span> n = poly.size();</span><br><span class="line">    <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; n; ++i)&#123;</span><br><span class="line">        <span class="keyword">if</span>(OnSegment(p, poly[i], poly[(i+<span class="number">1</span>)%n])) <span class="keyword">return</span> <span class="number">-1</span>;</span><br><span class="line">        <span class="keyword">int</span> k = sgn(Cross(poly[(i+<span class="number">1</span>)%n] - poly[i], p - poly[i]));</span><br><span class="line">        <span class="keyword">int</span> d1 = sgn(poly[i].y - p.y);</span><br><span class="line">        <span class="keyword">int</span> d2 = sgn(poly[(i+<span class="number">1</span>)%n].y - p.y);</span><br><span class="line">        <span class="keyword">if</span>(k &gt; <span class="number">0</span> &amp;&amp; d1 &lt;= <span class="number">0</span> &amp;&amp; d2 &gt; <span class="number">0</span>) wn++;</span><br><span class="line">        <span class="keyword">if</span>(k &lt; <span class="number">0</span> &amp;&amp; d2 &lt;= <span class="number">0</span> &amp;&amp; d1 &gt; <span class="number">0</span>) wn--;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">if</span>(wn != <span class="number">0</span>)</span><br><span class="line">        <span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="6-圆"><a href="#6-圆" class="headerlink" title="6 圆"></a>6 圆</h2><p>计算机中储存圆通常记录圆心坐标与半径即可</p>
<h3 id="6-1-定义"><a href="#6-1-定义" class="headerlink" title="6.1 定义"></a>6.1 定义</h3><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Circle</span>&#123;</span></span><br><span class="line">    Point c;</span><br><span class="line">    <span class="keyword">double</span> r;</span><br><span class="line">    Circle(Point c, <span class="keyword">double</span> r):c(c), r(r) &#123;&#125;</span><br><span class="line">    <span class="function">Point <span class="title">point</span><span class="params">(<span class="keyword">double</span> a)</span></span>&#123;<span class="comment">//通过圆心角求坐标</span></span><br><span class="line">        <span class="keyword">return</span> Point(c.x + <span class="built_in">cos</span>(a)*r, c.y + <span class="built_in">sin</span>(a)*r);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<h3 id="6-2-常用函数"><a href="#6-2-常用函数" class="headerlink" title="6.2 常用函数"></a>6.2 常用函数</h3><h4 id="6-2-1-圆与直线交点"><a href="#6-2-1-圆与直线交点" class="headerlink" title="6.2.1 圆与直线交点"></a>6.2.1 圆与直线交点</h4><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//求圆与直线交点</span></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">getLineCircleIntersection</span><span class="params">(Line L, Circle C, <span class="keyword">double</span>&amp; t1, <span class="keyword">double</span>&amp; t2, <span class="built_in">vector</span>&lt;Point&gt;&amp; sol)</span></span>&#123;</span><br><span class="line">    <span class="keyword">double</span> a = L.v.x, b = L.p.x - C.c.x, c = L.v.y, d = L.p.y - C.c.y;</span><br><span class="line">    <span class="keyword">double</span> e = a*a + c*c, f = <span class="number">2</span>*(a*b + c*d), g = b*b + d*d - C.r*C.r;</span><br><span class="line">    <span class="keyword">double</span> delta = f*f - <span class="number">4</span>*e*g;<span class="comment">//判别式</span></span><br><span class="line">    <span class="keyword">if</span>(sgn(delta) &lt; <span class="number">0</span>)<span class="comment">//相离</span></span><br><span class="line">        <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">    <span class="keyword">if</span>(sgn(delta) == <span class="number">0</span>)&#123;<span class="comment">//相切</span></span><br><span class="line">        t1 = -f /(<span class="number">2</span>*e);</span><br><span class="line">        t2 = -f /(<span class="number">2</span>*e);</span><br><span class="line">        sol.push_back(L.point(t1));<span class="comment">//sol存放交点本身</span></span><br><span class="line">        <span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">//相交</span></span><br><span class="line">    t1 = (-f - <span class="built_in">sqrt</span>(delta))/(<span class="number">2</span>*e);</span><br><span class="line">    sol.push_back(L.point(t1));</span><br><span class="line">    t2 = (-f + <span class="built_in">sqrt</span>(delta))/(<span class="number">2</span>*e);</span><br><span class="line">    sol.push_back(L.point(t2));</span><br><span class="line">    <span class="keyword">return</span> <span class="number">2</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h4 id="6-2-2-求两圆交点"><a href="#6-2-2-求两圆交点" class="headerlink" title="6.2.2 求两圆交点"></a>6.2.2 求两圆交点</h4><h4 id="6-2-3-点到圆的切线"><a href="#6-2-3-点到圆的切线" class="headerlink" title="6.2.3 点到圆的切线"></a>6.2.3 点到圆的切线</h4><h4 id="6-2-4-两圆的公切线"><a href="#6-2-4-两圆的公切线" class="headerlink" title="6.2.4 两圆的公切线"></a>6.2.4 两圆的公切线</h4><h4 id="6-2-5-两圆相交面积"><a href="#6-2-5-两圆相交面积" class="headerlink" title="6.2.5 两圆相交面积"></a>6.2.5 两圆相交面积</h4><p>通过计算两个圆相交所构成的两个扇形面积和减去其构成的筝形的面积</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">double</span> <span class="title">AreaOfOverlap</span><span class="params">(Point c1, <span class="keyword">double</span> r1, Point c2, <span class="keyword">double</span> r2)</span></span>&#123;</span><br><span class="line">    <span class="keyword">double</span> d = Length(c1 - c2);</span><br><span class="line">    <span class="keyword">if</span>(r1 + r2 &lt; d + eps)</span><br><span class="line">        <span class="keyword">return</span> <span class="number">0.0</span>;</span><br><span class="line">    <span class="keyword">if</span>(d &lt; <span class="built_in">fabs</span>(r1 - r2) + eps)&#123;</span><br><span class="line">        <span class="keyword">double</span> r = min(r1, r2);</span><br><span class="line">        <span class="keyword">return</span> pi*r*r;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">double</span> x = (d*d + r1*r1 - r2*r2)/(<span class="number">2.0</span>*d);</span><br><span class="line">    <span class="keyword">double</span> p = (r1 + r2 + d)/<span class="number">2.0</span>;</span><br><span class="line">    <span class="keyword">double</span> t1 = <span class="built_in">acos</span>(x/r1);</span><br><span class="line">    <span class="keyword">double</span> t2 = <span class="built_in">acos</span>((d - x)/r2);</span><br><span class="line">    <span class="keyword">double</span> s1 = r1*r1*t1;</span><br><span class="line">    <span class="keyword">double</span> s2 = r2*r2*t2;</span><br><span class="line">    <span class="keyword">double</span> s3 = <span class="number">2</span>*<span class="built_in">sqrt</span>(p*(p - r1)*(p - r2)*(p - d));</span><br><span class="line">    <span class="keyword">return</span> s1 + s2 - s3;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

      
    </div>

    

    
    
    

    

    
      
    
    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/ACM/" rel="tag"># ACM</a>
          
            <a href="/tags/计算几何/" rel="tag"># 计算几何</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/20190211/康托展开与逆康托展开/" rel="next" title="康托展开与逆康托展开">
                <i class="fa fa-chevron-left"></i> 康托展开与逆康托展开
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/20190211/ACM计算几何篇/" rel="prev" title="ACM计算几何篇">
                ACM计算几何篇 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>


  </div>


          </div>
          

  
    <div class="comments" id="comments">
    </div>

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope="" itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image" src="/images/star.jpg" alt="linxi">
            
              <p class="site-author-name" itemprop="name">linxi</p>
              <p class="site-description motion-element" itemprop="description">stay hungry, stay foolish</p>
          </div>

          
            <nav class="site-state motion-element">
              
                <div class="site-state-item site-state-posts">
                
                  <a href="/archives/">
                
                    <span class="site-state-item-count">15</span>
                    <span class="site-state-item-name">日志</span>
                  </a>
                </div>
              

              
                
                
                <div class="site-state-item site-state-categories">
                  <a href="/categories/index.html">
                    
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                    <span class="site-state-item-count">9</span>
                    <span class="site-state-item-name">分类</span>
                  </a>
                </div>
              

              
                
                
                <div class="site-state-item site-state-tags">
                  <a href="/tags/index.html">
                    
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                    <span class="site-state-item-count">13</span>
                    <span class="site-state-item-name">标签</span>
                  </a>
                </div>
              
            </nav>
          

          

          

          

          
          
            <div class="links-of-blogroll motion-element links-of-blogroll-block">
              <div class="links-of-blogroll-title">
                <i class="fa  fa-fw fa-link"></i>
                Links
              </div>
              <ul class="links-of-blogroll-list">
                
                  <li class="links-of-blogroll-item">
                    <a href="https://blog.csdn.net/linxilinxilinxi" title="https://blog.csdn.net/linxilinxilinxi" rel="noopener" target="_blank">My CSDN</a>
                  </li>
                
              </ul>
            </div>
          

          
            
          
          

        </div>
      </div>

      
      <!--noindex-->
        <div class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
            
            
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-前言"><span class="nav-number">1.</span> <span class="nav-text">1 前言</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-1-生产生活"><span class="nav-number">1.1.</span> <span class="nav-text">1.1 生产生活</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-2-ICPC竞赛"><span class="nav-number">1.2.</span> <span class="nav-text">1.2 ICPC竞赛</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-准备知识"><span class="nav-number">2.</span> <span class="nav-text">2 准备知识</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#2-1-头文件及函数及常量"><span class="nav-number">2.1.</span> <span class="nav-text">2.1 头文件及函数及常量</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-2-浮点误差"><span class="nav-number">2.2.</span> <span class="nav-text">2.2 浮点误差</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#2-2-1-计算误差"><span class="nav-number">2.2.1.</span> <span class="nav-text">2.2.1 计算误差</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-2-2-判等"><span class="nav-number">2.2.2.</span> <span class="nav-text">2.2.2 判等</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-2-2-1-解决方案-1-误差判别法"><span class="nav-number">2.2.3.</span> <span class="nav-text">2.2.2.1 解决方案 1 误差判别法</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-2-2-2-解决方案-2-化浮为整"><span class="nav-number">2.2.4.</span> <span class="nav-text">2.2.2.2 解决方案 2 化浮为整</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-2-3-负零"><span class="nav-number">2.2.5.</span> <span class="nav-text">2.2.3 负零</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-2-4-反三角函数"><span class="nav-number">2.2.6.</span> <span class="nav-text">2.2.4 反三角函数</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-3-模板总结"><span class="nav-number">2.3.</span> <span class="nav-text">2.3 模板总结</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-点与向量"><span class="nav-number">3.</span> <span class="nav-text">3 点与向量</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#3-2-相关定义"><span class="nav-number">3.1.</span> <span class="nav-text">3.2 相关定义</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#3-1-1-点的定义"><span class="nav-number">3.1.1.</span> <span class="nav-text">3.1.1 点的定义</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#3-1-2-向量的定义"><span class="nav-number">3.1.2.</span> <span class="nav-text">3.1.2 向量的定义</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-2-运算"><span class="nav-number">3.2.</span> <span class="nav-text">3.2 运算</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#3-2-1-加减乘除"><span class="nav-number">3.2.1.</span> <span class="nav-text">3.2.1 加减乘除</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#3-2-1-1-加法运算"><span class="nav-number">3.2.1.1.</span> <span class="nav-text">3.2.1.1 加法运算</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#3-2-1-2-减法运算"><span class="nav-number">3.2.1.2.</span> <span class="nav-text">3.2.1.2 减法运算</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#3-2-1-3-乘法运算"><span class="nav-number">3.2.1.3.</span> <span class="nav-text">3.2.1.3 乘法运算</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#3-2-1-4-除法运算"><span class="nav-number">3.2.1.4.</span> <span class="nav-text">3.2.1.4 除法运算</span></a></li></ol></li><li class="nav-item nav-level-4"><a class="nav-link" href="#3-2-2-小于运算（Left-Then-Low-排序）"><span class="nav-number">3.2.2.</span> <span class="nav-text">3.2.2 小于运算（Left Then Low 排序）</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#3-2-3-等于运算"><span class="nav-number">3.2.3.</span> <span class="nav-text">3.2.3 等于运算</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#3-2-4-内积运算"><span class="nav-number">3.2.4.</span> <span class="nav-text">3.2.4  内积运算</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#3-2-4-1-几何意义"><span class="nav-number">3.2.4.1.</span> <span class="nav-text">3.2.4.1 几何意义</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#3-2-4-2-代码实现"><span class="nav-number">3.2.4.2.</span> <span class="nav-text">3.2.4.2 代码实现</span></a></li></ol></li><li class="nav-item nav-level-4"><a class="nav-link" href="#3-2-5-外积运算"><span class="nav-number">3.2.5.</span> <span class="nav-text">3.2.5 外积运算</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#3-2-5-1-几何意义"><span class="nav-number">3.2.5.1.</span> <span class="nav-text">3.2.5.1 几何意义</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#3-2-5-2-判断外积的符号"><span class="nav-number">3.2.5.2.</span> <span class="nav-text">3.2.5.2 判断外积的符号</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#3-2-5-3-代码实现"><span class="nav-number">3.2.5.3.</span> <span class="nav-text">3.2.5.3 代码实现</span></a></li></ol></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-3-常用函数"><span class="nav-number">3.3.</span> <span class="nav-text">3.3 常用函数</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#3-3-1-取模（长度）"><span class="nav-number">3.3.1.</span> <span class="nav-text">3.3.1 取模（长度）</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#3-3-2-计算两向量夹角"><span class="nav-number">3.3.2.</span> <span class="nav-text">3.3.2 计算两向量夹角</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#3-3-3-计算两向量构成的平行四边形有向面积"><span class="nav-number">3.3.3.</span> <span class="nav-text">3.3.3 计算两向量构成的平行四边形有向面积</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#3-3-4-计算向量逆时针旋转后的向量"><span class="nav-number">3.3.4.</span> <span class="nav-text">3.3.4 计算向量逆时针旋转后的向量</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#3-3-5-计算向量逆时针旋转九十度的单位法向量"><span class="nav-number">3.3.5.</span> <span class="nav-text">3.3.5 计算向量逆时针旋转九十度的单位法向量</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#3-3-6-ToLeftTest"><span class="nav-number">3.3.6.</span> <span class="nav-text">3.3.6 ToLeftTest</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-4-复数黑科技"><span class="nav-number">3.4.</span> <span class="nav-text">3.4 复数黑科技</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#3-4-1-代码实现"><span class="nav-number">3.4.1.</span> <span class="nav-text">3.4.1 代码实现</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#3-4-2-不足"><span class="nav-number">3.4.2.</span> <span class="nav-text">3.4.2 不足</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-5-模板总结"><span class="nav-number">3.5.</span> <span class="nav-text">3.5 模板总结</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#3-5-1-手动实现"><span class="nav-number">3.5.1.</span> <span class="nav-text">3.5.1 手动实现</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#3-5-2-复数黑科技"><span class="nav-number">3.5.2.</span> <span class="nav-text">3.5.2 复数黑科技</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-点与线"><span class="nav-number">4.</span> <span class="nav-text">4 点与线</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#4-1-定义"><span class="nav-number">4.1.</span> <span class="nav-text">4.1 定义</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#4-1-1-直线定义"><span class="nav-number">4.1.1.</span> <span class="nav-text">4.1.1 直线定义</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#4-1-2-点向式"><span class="nav-number">4.1.2.</span> <span class="nav-text">4.1.2 点向式</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#4-1-2-1-优点"><span class="nav-number">4.1.2.1.</span> <span class="nav-text">4.1.2.1 优点</span></a></li></ol></li><li class="nav-item nav-level-4"><a class="nav-link" href="#4-1-3-线段与射线"><span class="nav-number">4.1.3.</span> <span class="nav-text">4.1.3 线段与射线</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#4-2-实现"><span class="nav-number">4.2.</span> <span class="nav-text">4.2 实现</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#4-3-常用操作"><span class="nav-number">4.3.</span> <span class="nav-text">4.3 常用操作</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#4-3-1-判断点在直线上"><span class="nav-number">4.3.1.</span> <span class="nav-text">4.3.1 判断点在直线上</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#4-3-2-计算两直线交点"><span class="nav-number">4.3.2.</span> <span class="nav-text">4.3.2 计算两直线交点</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#4-3-3-计算点到直线的距离"><span class="nav-number">4.3.3.</span> <span class="nav-text">4.3.3 计算点到直线的距离</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#4-3-4-计算点到线段的距离"><span class="nav-number">4.3.4.</span> <span class="nav-text">4.3.4 计算点到线段的距离</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#4-3-5-求点在直线上的投影点"><span class="nav-number">4.3.5.</span> <span class="nav-text">4.3.5 求点在直线上的投影点</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#4-3-6-判断点是否在线段上"><span class="nav-number">4.3.6.</span> <span class="nav-text">4.3.6 判断点是否在线段上</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#4-3-7-判断两线段是否相交"><span class="nav-number">4.3.7.</span> <span class="nav-text">4.3.7 判断两线段是否相交</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#4-4-模板总结"><span class="nav-number">4.4.</span> <span class="nav-text">4.4 模板总结</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#5-多边形"><span class="nav-number">5.</span> <span class="nav-text">5 多边形</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#5-1-三角形"><span class="nav-number">5.1.</span> <span class="nav-text">5.1 三角形</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#5-1-1-三角形面积"><span class="nav-number">5.1.1.</span> <span class="nav-text">5.1.1 三角形面积</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#5-1-2-三角形四心"><span class="nav-number">5.1.2.</span> <span class="nav-text">5.1.2 三角形四心</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#5-1-2-1-外心"><span class="nav-number">5.1.2.1.</span> <span class="nav-text">5.1.2.1 外心</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#5-1-2-2-内心"><span class="nav-number">5.1.2.2.</span> <span class="nav-text">5.1.2.2 内心</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#5-1-2-3-垂心"><span class="nav-number">5.1.2.3.</span> <span class="nav-text">5.1.2.3 垂心</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#5-1-2-4-重心"><span class="nav-number">5.1.2.4.</span> <span class="nav-text">5.1.2.4 重心</span></a></li></ol></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#5-2-普通多边形"><span class="nav-number">5.2.</span> <span class="nav-text">5.2 普通多边形</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#5-2-1-定义"><span class="nav-number">5.2.1.</span> <span class="nav-text">5.2.1 定义</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#5-2-1-1-多边形"><span class="nav-number">5.2.1.1.</span> <span class="nav-text">5.2.1.1 多边形</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#5-2-1-2-简单多边形"><span class="nav-number">5.2.1.2.</span> <span class="nav-text">5.2.1.2 简单多边形</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#5-2-1-3-凸多边形"><span class="nav-number">5.2.1.3.</span> <span class="nav-text">5.2.1.3 凸多边形</span></a></li></ol></li><li class="nav-item nav-level-4"><a class="nav-link" href="#5-2-2-常用函数"><span class="nav-number">5.2.2.</span> <span class="nav-text">5.2.2 常用函数</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#5-2-2-1-求多边形面积"><span class="nav-number">5.2.2.1.</span> <span class="nav-text">5.2.2.1 求多边形面积</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#5-2-2-2-判断点在多边形内"><span class="nav-number">5.2.2.2.</span> <span class="nav-text">5.2.2.2 判断点在多边形内</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#5-2-2-4-判断点在凸多边形内"><span class="nav-number">5.2.2.3.</span> <span class="nav-text">5.2.2.4 判断点在凸多边形内</span></a></li></ol></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#5-3-Pick定理"><span class="nav-number">5.3.</span> <span class="nav-text">5.3 Pick定理</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#5-3-1-内容"><span class="nav-number">5.3.1.</span> <span class="nav-text">5.3.1 内容</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#5-3-2-常用计算"><span class="nav-number">5.3.2.</span> <span class="nav-text">5.3.2 常用计算</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#5-4-模板总结"><span class="nav-number">5.4.</span> <span class="nav-text">5.4 模板总结</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#6-圆"><span class="nav-number">6.</span> <span class="nav-text">6 圆</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#6-1-定义"><span class="nav-number">6.1.</span> <span class="nav-text">6.1 定义</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#6-2-常用函数"><span class="nav-number">6.2.</span> <span class="nav-text">6.2 常用函数</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#6-2-1-圆与直线交点"><span class="nav-number">6.2.1.</span> <span class="nav-text">6.2.1 圆与直线交点</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#6-2-2-求两圆交点"><span class="nav-number">6.2.2.</span> <span class="nav-text">6.2.2 求两圆交点</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#6-2-3-点到圆的切线"><span class="nav-number">6.2.3.</span> <span class="nav-text">6.2.3 点到圆的切线</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#6-2-4-两圆的公切线"><span class="nav-number">6.2.4.</span> <span class="nav-text">6.2.4 两圆的公切线</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#6-2-5-两圆相交面积"><span class="nav-number">6.2.5.</span> <span class="nav-text">6.2.5 两圆相交面积</span></a></li></ol></li></ol></li></ol></div>
            

          </div>
        </div>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; 2019 – <span itemprop="copyrightYear">2020</span>
  <span class="with-love" id="animate">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">linxi</span>

  

  
</div>


  <div class="powered-by">由 <a href="https://hexo.io" class="theme-link" rel="noopener" target="_blank">Hexo</a> 强力驱动 v3.8.0</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 – <a href="https://theme-next.org" class="theme-link" rel="noopener" target="_blank">NexT.Pisces</a> v7.0.0</div>





<div class="theme-info">
  <div class="powered-by"></div>
  <span class="post-count">全站共 65.5k 字</span>
</div>

        








        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

    

    
  </div>

  

<script>
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>














  
    
    
  
  <script color="25,202,173" opacity="0.8" zindex="-1" count="200" src="/lib/canvas-nest/canvas-nest.min.js"></script>













  
  <script src="/lib/jquery/index.js?v=2.1.3"></script>

  
  <script src="/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>


  


  <script src="/js/src/utils.js?v=7.0.0"></script>

  <script src="/js/src/motion.js?v=7.0.0"></script>



  
  


  <script src="/js/src/affix.js?v=7.0.0"></script>

  <script src="/js/src/schemes/pisces.js?v=7.0.0"></script>



  
  <script src="/js/src/scrollspy.js?v=7.0.0"></script>
<script src="/js/src/post-details.js?v=7.0.0"></script>



  


  <script src="/js/src/bootstrap.js?v=7.0.0"></script>



  
  

<script src="//cdn1.lncld.net/static/js/3.11.1/av-min.js"></script>



<script src="//unpkg.com/valine/dist/Valine.min.js"></script>

<script>
  var GUEST = ['nick', 'mail', 'link'];
  var guest = 'nick,mail,link';
  guest = guest.split(',').filter(function(item) {
    return GUEST.indexOf(item) > -1;
  });
  new Valine({
    el: '#comments',
    verify: false,
    notify: false,
    appId: 'LwzdQrGG81bUwkUkAfdPexj1-gzGzoHsz',
    appKey: 'akTaTqH7XeeKc6CPRfB7vbDl',
    placeholder: 'Just go go',
    avatar: 'mm',
    meta: guest,
    pageSize: '10' || 10,
    visitor: false
  });
</script>




  


  
  <script>
    // Popup Window;
    var isfetched = false;
    var isXml = true;
    // Search DB path;
    var search_path = "search.xml";
    if (search_path.length === 0) {
      search_path = "search.xml";
    } else if (/json$/i.test(search_path)) {
      isXml = false;
    }
    var path = "/" + search_path;
    // monitor main search box;

    var onPopupClose = function (e) {
      $('.popup').hide();
      $('#local-search-input').val('');
      $('.search-result-list').remove();
      $('#no-result').remove();
      $(".local-search-pop-overlay").remove();
      $('body').css('overflow', '');
    }

    function proceedsearch() {
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay"></div>')
        .css('overflow', 'hidden');
      $('.search-popup-overlay').click(onPopupClose);
      $('.popup').toggle();
      var $localSearchInput = $('#local-search-input');
      $localSearchInput.attr("autocapitalize", "none");
      $localSearchInput.attr("autocorrect", "off");
      $localSearchInput.focus();
    }

    // search function;
    var searchFunc = function(path, search_id, content_id) {
      'use strict';

      // start loading animation
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay">' +
          '<div id="search-loading-icon">' +
          '<i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>' +
          '</div>' +
          '</div>')
        .css('overflow', 'hidden');
      $("#search-loading-icon").css('margin', '20% auto 0 auto').css('text-align', 'center');

      

      $.ajax({
        url: path,
        dataType: isXml ? "xml" : "json",
        async: true,
        success: function(res) {
          // get the contents from search data
          isfetched = true;
          $('.popup').detach().appendTo('.header-inner');
          var datas = isXml ? $("entry", res).map(function() {
            return {
              title: $("title", this).text(),
              content: $("content",this).text(),
              url: $("url" , this).text()
            };
          }).get() : res;
          var input = document.getElementById(search_id);
          var resultContent = document.getElementById(content_id);
          var inputEventFunction = function() {
            var searchText = input.value.trim().toLowerCase();
            var keywords = searchText.split(/[\s\-]+/);
            if (keywords.length > 1) {
              keywords.push(searchText);
            }
            var resultItems = [];
            if (searchText.length > 0) {
              // perform local searching
              datas.forEach(function(data) {
                var isMatch = false;
                var hitCount = 0;
                var searchTextCount = 0;
                var title = data.title.trim();
                var titleInLowerCase = title.toLowerCase();
                var content = data.content.trim().replace(/<[^>]+>/g,"");
                
                var contentInLowerCase = content.toLowerCase();
                var articleUrl = decodeURIComponent(data.url).replace(/\/{2,}/g, '/');
                var indexOfTitle = [];
                var indexOfContent = [];
                // only match articles with not empty titles
                if(title != '') {
                  keywords.forEach(function(keyword) {
                    function getIndexByWord(word, text, caseSensitive) {
                      var wordLen = word.length;
                      if (wordLen === 0) {
                        return [];
                      }
                      var startPosition = 0, position = [], index = [];
                      if (!caseSensitive) {
                        text = text.toLowerCase();
                        word = word.toLowerCase();
                      }
                      while ((position = text.indexOf(word, startPosition)) > -1) {
                        index.push({position: position, word: word});
                        startPosition = position + wordLen;
                      }
                      return index;
                    }

                    indexOfTitle = indexOfTitle.concat(getIndexByWord(keyword, titleInLowerCase, false));
                    indexOfContent = indexOfContent.concat(getIndexByWord(keyword, contentInLowerCase, false));
                  });
                  if (indexOfTitle.length > 0 || indexOfContent.length > 0) {
                    isMatch = true;
                    hitCount = indexOfTitle.length + indexOfContent.length;
                  }
                }

                // show search results

                if (isMatch) {
                  // sort index by position of keyword

                  [indexOfTitle, indexOfContent].forEach(function (index) {
                    index.sort(function (itemLeft, itemRight) {
                      if (itemRight.position !== itemLeft.position) {
                        return itemRight.position - itemLeft.position;
                      } else {
                        return itemLeft.word.length - itemRight.word.length;
                      }
                    });
                  });

                  // merge hits into slices

                  function mergeIntoSlice(text, start, end, index) {
                    var item = index[index.length - 1];
                    var position = item.position;
                    var word = item.word;
                    var hits = [];
                    var searchTextCountInSlice = 0;
                    while (position + word.length <= end && index.length != 0) {
                      if (word === searchText) {
                        searchTextCountInSlice++;
                      }
                      hits.push({position: position, length: word.length});
                      var wordEnd = position + word.length;

                      // move to next position of hit

                      index.pop();
                      while (index.length != 0) {
                        item = index[index.length - 1];
                        position = item.position;
                        word = item.word;
                        if (wordEnd > position) {
                          index.pop();
                        } else {
                          break;
                        }
                      }
                    }
                    searchTextCount += searchTextCountInSlice;
                    return {
                      hits: hits,
                      start: start,
                      end: end,
                      searchTextCount: searchTextCountInSlice
                    };
                  }

                  var slicesOfTitle = [];
                  if (indexOfTitle.length != 0) {
                    slicesOfTitle.push(mergeIntoSlice(title, 0, title.length, indexOfTitle));
                  }

                  var slicesOfContent = [];
                  while (indexOfContent.length != 0) {
                    var item = indexOfContent[indexOfContent.length - 1];
                    var position = item.position;
                    var word = item.word;
                    // cut out 100 characters
                    var start = position - 20;
                    var end = position + 80;
                    if(start < 0){
                      start = 0;
                    }
                    if (end < position + word.length) {
                      end = position + word.length;
                    }
                    if(end > content.length){
                      end = content.length;
                    }
                    slicesOfContent.push(mergeIntoSlice(content, start, end, indexOfContent));
                  }

                  // sort slices in content by search text's count and hits' count

                  slicesOfContent.sort(function (sliceLeft, sliceRight) {
                    if (sliceLeft.searchTextCount !== sliceRight.searchTextCount) {
                      return sliceRight.searchTextCount - sliceLeft.searchTextCount;
                    } else if (sliceLeft.hits.length !== sliceRight.hits.length) {
                      return sliceRight.hits.length - sliceLeft.hits.length;
                    } else {
                      return sliceLeft.start - sliceRight.start;
                    }
                  });

                  // select top N slices in content

                  var upperBound = parseInt('1');
                  if (upperBound >= 0) {
                    slicesOfContent = slicesOfContent.slice(0, upperBound);
                  }

                  // highlight title and content

                  function highlightKeyword(text, slice) {
                    var result = '';
                    var prevEnd = slice.start;
                    slice.hits.forEach(function (hit) {
                      result += text.substring(prevEnd, hit.position);
                      var end = hit.position + hit.length;
                      result += '<b class="search-keyword">' + text.substring(hit.position, end) + '</b>';
                      prevEnd = end;
                    });
                    result += text.substring(prevEnd, slice.end);
                    return result;
                  }

                  var resultItem = '';

                  if (slicesOfTitle.length != 0) {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + highlightKeyword(title, slicesOfTitle[0]) + "</a>";
                  } else {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + title + "</a>";
                  }

                  slicesOfContent.forEach(function (slice) {
                    resultItem += "<a href='" + articleUrl + "'>" +
                      "<p class=\"search-result\">" + highlightKeyword(content, slice) +
                      "...</p>" + "</a>";
                  });

                  resultItem += "</li>";
                  resultItems.push({
                    item: resultItem,
                    searchTextCount: searchTextCount,
                    hitCount: hitCount,
                    id: resultItems.length
                  });
                }
              })
            };
            if (keywords.length === 1 && keywords[0] === "") {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-search fa-5x"></i></div>'
            } else if (resultItems.length === 0) {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-frown-o fa-5x"></i></div>'
            } else {
              resultItems.sort(function (resultLeft, resultRight) {
                if (resultLeft.searchTextCount !== resultRight.searchTextCount) {
                  return resultRight.searchTextCount - resultLeft.searchTextCount;
                } else if (resultLeft.hitCount !== resultRight.hitCount) {
                  return resultRight.hitCount - resultLeft.hitCount;
                } else {
                  return resultRight.id - resultLeft.id;
                }
              });
              var searchResultList = '<ul class=\"search-result-list\">';
              resultItems.forEach(function (result) {
                searchResultList += result.item;
              })
              searchResultList += "</ul>";
              resultContent.innerHTML = searchResultList;
            }
          }

          if ('auto' === 'auto') {
            input.addEventListener('input', inputEventFunction);
          } else {
            $('.search-icon').click(inputEventFunction);
            input.addEventListener('keypress', function (event) {
              if (event.keyCode === 13) {
                inputEventFunction();
              }
            });
          }

          // remove loading animation
          $(".local-search-pop-overlay").remove();
          $('body').css('overflow', '');

          proceedsearch();
        }
      });
    }

    // handle and trigger popup window;
    $('.popup-trigger').click(function(e) {
      e.stopPropagation();
      if (isfetched === false) {
        searchFunc(path, 'local-search-input', 'local-search-result');
      } else {
        proceedsearch();
      };
    });

    $('.popup-btn-close').click(onPopupClose);
    $('.popup').click(function(e){
      e.stopPropagation();
    });
    $(document).on('keyup', function (event) {
      var shouldDismissSearchPopup = event.which === 27 &&
        $('.search-popup').is(':visible');
      if (shouldDismissSearchPopup) {
        onPopupClose();
      }
    });
  </script>





  
  
  <script>
    
    function addCount(Counter) {
      var $visitors = $('.leancloud_visitors');
      var url = $visitors.attr('id').trim();
      var title = $visitors.attr('data-flag-title').trim();

      Counter('get', '/classes/Counter', { where: JSON.stringify({ url }) })
        .done(function({ results }) {
          if (results.length > 0) {
            var counter = results[0];
            
            Counter('put', '/classes/Counter/' + counter.objectId, JSON.stringify({ time: { '__op': 'Increment', 'amount': 1 } }))
            
              .done(function() {
                var $element = $(document.getElementById(url));
                $element.find('.leancloud-visitors-count').text(counter.time + 1);
              })
            
              .fail(function ({ responseJSON }) {
                console.log(`Failed to save Visitor num, with error message: ${responseJSON.error}`);
              })
          } else {
            
              Counter('post', '/classes/Counter', JSON.stringify({ title: title, url: url, time: 1 }))
                .done(function() {
                  var $element = $(document.getElementById(url));
                  $element.find('.leancloud-visitors-count').text(1);
                })
                .fail(function() {
                  console.log('Failed to create');
                });
            
          }
        })
        .fail(function ({ responseJSON }) {
          console.log(`LeanCloud Counter Error: ${responseJSON.code} ${responseJSON.error}`);
        });
    }
    

    $(function() {
      $.get('https://app-router.leancloud.cn/2/route?appId=' + 'LwzdQrGG81bUwkUkAfdPexj1-gzGzoHsz')
        .done(function({ api_server }) {
          var Counter = function(method, url, data) {
            return $.ajax({
              method: method,
              url: 'https://' + api_server + '/1.1' + url,
              headers: {
                'X-LC-Id': 'LwzdQrGG81bUwkUkAfdPexj1-gzGzoHsz',
                'X-LC-Key': 'akTaTqH7XeeKc6CPRfB7vbDl',
                'Content-Type': 'application/json',
              },
              data: data
            });
          };
          
            addCount(Counter);
          
        });
    });
  </script>



  

  
  

  
  

  
    
      <script type="text/x-mathjax-config">
  

  MathJax.Hub.Config({
    tex2jax: {
      inlineMath: [ ['$','$'], ["\\(","\\)"] ],
      processEscapes: true,
      skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
    },
    TeX: {
      
      equationNumbers: {
        autoNumber: "AMS"
      }
    }
  });
</script>

<script type="text/x-mathjax-config">
  MathJax.Hub.Queue(function() {
    var all = MathJax.Hub.getAllJax(), i;
      for (i = 0; i < all.length; i += 1) {
        all[i].SourceElement().parentNode.className += ' has-jax';
      }
  });
</script>
<script src="//cdn.jsdelivr.net/npm/mathjax@2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>

<style>
.MathJax_Display {
  overflow: auto hidden;
}
</style>

    
  


  

  

  

  

  

  

  

  

  

  

</body>
</html>

<!-- 页面点击小红心 -->
<script type="text/javascript" src="/js/src/clicklove.js"></script>

